package com.hanbang.mrcm_merchant.timeselect;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.hanbang.mrcm_merchant.utils.MyLog;
import com.hanbang.mrcm_merchant.utils.ScreenInfo;

import android.app.Activity;

public class DateWheelMain {

	private WheelView wv_year;
	private WheelView wv_month;
	private WheelView wv_day;
	private WheelView wv_hour;
	private WheelView wv_min;
	Calendar calendar = null;
	public int screenheight;
	private static int START_YEAR = 1900, END_YEAR = 2100;

	public static int getSTART_YEAR() {
		return START_YEAR;
	}

	public static void setSTART_YEAR(int sTART_YEAR) {
		START_YEAR = sTART_YEAR;
	}

	public static int getEND_YEAR() {
		return END_YEAR;
	}

	public static void setEND_YEAR(int eND_YEAR) {
		END_YEAR = eND_YEAR;
	}

	public DateWheelMain(Activity context, WheelView... year) {
		try {
			wv_year = year[0];
			wv_month = year[1];
			wv_day = year[2];
			wv_hour = year[3];
			wv_min = year[4];
		} catch (IndexOutOfBoundsException e) {
			e.printStackTrace();
		}
		calendar = Calendar.getInstance();
		screenheight = new ScreenInfo(context).getHeight();
		initDateTimePicker();
	}

	public void setTime(Calendar calendar) {
		this.calendar = calendar;
		initDateTimePicker();
	}

	/**
	 * @Description: TODO 弹出日期时间选择器 -1:代表不显示这个单位
	 */
	public void initDateTimePicker() {
		// 根据屏幕密度来指定选择器字体的大小(不同屏幕可能不同)
		int textSize = (screenheight / 100) * 3;
		// 添加大小月月份并将其转换为list,方便之后的判断
		final List<String> list_big = Arrays.asList(new String[] { "1", "3",
				"5", "7", "8", "10", "12" });
		final List<String> list_little = Arrays.asList(new String[] { "4", "6",
				"9", "11" });
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int min = calendar.get(Calendar.MINUTE);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);

		// 年
		if (wv_year != null) {
			wv_year.setAdapter(new NumericWheelAdapter(START_YEAR, END_YEAR));// 设置"年"的显示数据
			wv_year.setCyclic(true);// 可循环滚动
			wv_year.setLabel("年"); // 添加文字
			wv_year.setCurrentItem(year - START_YEAR);// 初始化时显示的数据
			wv_year.TEXT_SIZE = textSize;
		}

		// 月
		if (wv_month != null) {
			wv_month.setAdapter(new NumericWheelAdapter(1, 12));
			wv_month.setCyclic(true);
			wv_month.setLabel("月");
			wv_month.setCurrentItem(month);
			wv_month.TEXT_SIZE = textSize;
		}

		// 日
		if (wv_day != null) {
			wv_day.setCyclic(true);
			// 判断大小月及是否闰年,用来确定"日"的数据
			if (list_big.contains(String.valueOf(month + 1))) {
				wv_day.setAdapter(new NumericWheelAdapter(1, 31));
			} else if (list_little.contains(String.valueOf(month + 1))) {
				wv_day.setAdapter(new NumericWheelAdapter(1, 30));
			} else {
				// 闰年
				if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
					wv_day.setAdapter(new NumericWheelAdapter(1, 29));
				else
					wv_day.setAdapter(new NumericWheelAdapter(1, 28));
			}
			wv_day.setLabel("日");
			wv_day.setCurrentItem(day - 1);
			wv_day.TEXT_SIZE = textSize;
		}
		// 时
		if (wv_hour != null) {
			wv_hour.setAdapter(new NumericWheelAdapter(1, 24));// 设置"年"的显示数据
			wv_hour.setCyclic(true);// 可循环滚动
			wv_hour.setLabel("时"); // 添加文字
			wv_hour.setCurrentItem(hour - 1);// 初始化时显示的数据
			wv_hour.TEXT_SIZE = textSize;
		}
		// 分
		if (wv_min != null) {
			wv_min.setAdapter(new NumericWheelAdapter(1, 60));// 设置"年"的显示数据
			wv_min.setCyclic(true);// 可循环滚动
			wv_min.setLabel("分"); // 添加文字
			wv_min.setCurrentItem(min);// 初始化时显示的数据
			wv_min.TEXT_SIZE = textSize;
		}

		// 添加"年"监听
		OnWheelChangedListener wheelListener_year = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				int year_num = newValue + START_YEAR;
				// 判断大小月及是否闰年,用来确定"日"的数据
				if (list_big
						.contains(String.valueOf(wv_month.getCurrentItem() + 1))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 31));
				} else if (list_little.contains(String.valueOf(wv_month
						.getCurrentItem() + 1))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 30));
				} else {
					if ((year_num % 4 == 0 && year_num % 100 != 0)
							|| year_num % 400 == 0)
						wv_day.setAdapter(new NumericWheelAdapter(1, 29));
					else
						wv_day.setAdapter(new NumericWheelAdapter(1, 28));
				}
			}
		};
		// 添加"月"监听
		OnWheelChangedListener wheelListener_month = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				int month_num = newValue + 1;
				// 判断大小月及是否闰年,用来确定"日"的数据
				if (list_big.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 31));
				} else if (list_little.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 30));
				} else if (wv_year != null) {
					if (((wv_year.getCurrentItem() + START_YEAR) % 4 == 0 && (wv_year
							.getCurrentItem() + START_YEAR) % 100 != 0)
							|| (wv_year.getCurrentItem() + START_YEAR) % 400 == 0)
						wv_day.setAdapter(new NumericWheelAdapter(1, 29));
					else
						wv_day.setAdapter(new NumericWheelAdapter(1, 28));
				}
			}
		};
		if (wv_year != null) {
			wv_year.addChangingListener(wheelListener_year);
		}
		if (wv_month != null) {
			wv_month.addChangingListener(wheelListener_month);
		}

	}

	public Calendar getTime() {
		Calendar c = Calendar.getInstance();
		if (wv_year != null) {
			c.set(Calendar.YEAR, wv_year.getCurrentItem() + START_YEAR);
		}
		if (wv_month != null) {
			c.set(Calendar.MONTH, wv_month.getCurrentItem());
		}
		if (wv_day != null) {
			c.set(Calendar.DAY_OF_MONTH, wv_day.getCurrentItem() + 1);
		}
		if (wv_hour != null) {
			c.set(Calendar.HOUR_OF_DAY, wv_hour.getCurrentItem() + 1);
		}
		if (wv_min != null) {
			c.set(Calendar.MINUTE, wv_min.getCurrentItem() + 1);
		}
		return c;
	}
}
